import { getAllRoleList, isAccountExist } from '@/api/demo/system';
import { BasicColumn, FormSchema } from '@/components/Table';
import { h } from 'vue';
import { Switch } from 'ant-design-vue';
import { setRoleStatus } from '@/api/demo/system';
import { useMessage } from '@/hooks/web/useMessage';
import { Tinymce } from '@/components/Tinymce';
import dayjs from 'dayjs';
import { uploadApi } from '@/api/sys/upload';

type CheckedType = boolean | string | number;

/**
 * transform mock data
 * {
 *  0: '华东分部',
 * '0-0': '华东分部-研发部'
 * '0-1': '华东分部-市场部',
 *  ...
 * }
 */
export const deptMap = (() => {
  const pDept = ['华东分部', '华南分部', '西北分部'];
  const cDept = ['研发部', '市场部', '商务部', '财务部'];

  return pDept.reduce((map, p, pIdx) => {
    map[pIdx] = p;

    cDept.forEach((c, cIndex) => (map[`${pIdx}-${cIndex}`] = `${p}-${c}`));

    return map;
  }, {});
})();

export const columns: BasicColumn[] = [
  {
    title: '所属栏目',
    dataIndex: 'dept',
    customRender: ({ value }) => {
      return deptMap[value];
    },
    width: 120,
  },
  {
    title: '所在模板',
    dataIndex: 'account',
    align: 'left',
    width: 180,
  },
  {
    title: '采集站点',
    dataIndex: 'website',
    align: 'left',
    width: 280,
  },
  {
    title: '采集频率',
    dataIndex: 'cron',
    align: 'left',
    width: 180,
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 120,
    sorter: true,
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.status === '1',
        checkedChildren: '已开启',
        unCheckedChildren: '未开启',
        loading: record.pendingStatus,
        onChange(checked: CheckedType) {
          record.pendingStatus = true;
          const newStatus = checked ? '1' : '0';
          const { createMessage } = useMessage();
          setRoleStatus(record.id, newStatus)
            .then(() => {
              record.status = newStatus;
              createMessage.success(`已成功修改角色状态`);
            })
            .catch(() => {
              createMessage.error('修改角色状态失败');
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '创建时间',
    dataIndex: 'id',
    width: 180,
    sorter: true,
    fixed: 'right',
    customRender: ({record})=>{
      return record.createTime;
    }
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    label: '采集源模板',
    field: 'templateId',
    component: 'ApiSelect',
    componentProps: {
      api: getAllRoleList,
      labelField: 'roleName',
      valueField: 'roleValue',
    },
    required: true,
    colProps: { xs: 24, xl: 12, lg: 8, xxl: 6 },
  },
  {
    field: 'url',
    label: '采集源地址',
    component: 'Input',
    colProps: { xs: 24, xl: 12, lg: 8, xxl: 6 },
  },
];

export const accountFormSchema: FormSchema[] = [
  
  {
    field: 'dept',
    label: '所属栏目',
    component: 'TreeSelect',
    componentProps: {
      fieldNames: {
        label: 'deptName',
        value: 'id',
      },
      getPopupContainer: () => document.body,
    },
    required: true,
  },
  {
    label: '采集源模板',
    field: 'templateId',
    component: 'ApiSelect',
    componentProps: {
      api: getAllRoleList,
      labelField: 'roleName',
      valueField: 'roleValue',
    },
    required: true
  },
  {
    label: '采集URL',
    field: 'url',
    component: 'Input',
    required: true
  },
  {
    label: '采集频率',
    field: 'cron',
    component: 'Input',
    required: true,
    slot: 'cron'
  },
  // {
  //   label: '角色',
  //   field: 'role',
  //   component: 'ApiSelect',
  //   componentProps: {
  //     api: getAllRoleList,
  //     labelField: 'roleName',
  //     valueField: 'roleValue',
  //   },
  //   required: true,
  // },
  {
    field: 'status',
    label: '状态',
    component: 'RadioButtonGroup',
    defaultValue: '1',
    componentProps: {
      options: [
        { label: '启用', value: '1' },
        { label: '停用', value: '0' },
      ],
    },
  },
  
];
